package com.tz.controller;

import com.tz.entity.ResearchTemplate;
import com.tz.service.ResearchTemplateService;
import com.tz.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * Created by Administrator on 2018/1/8 0008.
 */
@Controller
@RequestMapping("/evaltempl")
public class CheckTemplateController {

    @Autowired
    ResearchTemplateService researchTemplateService;

    @ResponseBody
    @RequestMapping(value = "/templtelist", produces = "application/json")
    public PageBean<ResearchTemplate> getAllResearchTemplate(@RequestParam("pageNow") Integer pageNow) {
        Integer pageSize = 5;
        PageBean<ResearchTemplate> templatePageBean = researchTemplateService.findResearchTemplateByPage(pageNow, pageSize);
        System.out.println(">>>.>>>>>templatePageBean=" + templatePageBean);
        return templatePageBean;
    }

    @RequestMapping("/pre")
    public String showResearch(HttpServletRequest request, Model model, @RequestParam("id") Long id) throws UnsupportedEncodingException {

        ResearchTemplate researchTemplate = researchTemplateService.getResearchTemplate(id);
        model.addAttribute("cate", researchTemplate.getQuestioncategories());
        model.addAttribute("adv", researchTemplate.getResearchadvices());
        return "pre";

    }

    @ResponseBody
    @RequestMapping(value = "/change", produces = "application/json")
    public String useStart(@RequestBody String ids) {
        System.out.println(ids);
        boolean isError = false;
        String[] idss = ids.split(",");
        Long[] templids = new Long[idss.length];
        for (int i = 0; i < idss.length; i++) {
            templids[i] = Long.parseLong(idss[i]);
            ResearchTemplate researchTemplate = researchTemplateService.getResearchTemplate(templids[i]);
            if (researchTemplate.getStatus() == 1) {

                    researchTemplate.setStatus(2);
            } else {
                if (researchTemplateService.countUseTime(templids[i])==0){
                    researchTemplate.setStatus(1);
                }else{
                    isError=true;
                }

            }
            researchTemplateService.alterTemplate(researchTemplate);
        }
        if (isError) {
            return "x";
        } else {
            return "ok";
        }

    }

    @ResponseBody
    @RequestMapping(value = "/delete", produces = "application/json")
    public String delete(@RequestBody String ids) {
        String[] idss = ids.split(",");
        Long[] templids = new Long[idss.length];
        for (int i = 0; i < idss.length; i++) {
            templids[i] = Long.parseLong(idss[i]);
            ResearchTemplate researchTemplate = researchTemplateService.getResearchTemplate(templids[i]);
            if (researchTemplate.getStatus() != 2) {
                researchTemplateService.removeTemplate(templids[i]);
            }

        }
        return "ok";
    }

}
